package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.JournalBean;
import com.project.bean.UserBean;
import com.project.mapper.IJournalMapper;
import com.project.mapper.IPowerMapper;
import com.project.mapper.IRoleMapper;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private IRoleMapper roleMapper;
    @Autowired
    private IPowerMapper powerMapper;
    @Autowired
    private IJournalMapper journalMapper;
    @Override
    public UserBean login(String userName, String pwd) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_username", userName);
        qw.eq("u_pwd", pwd);
        //登陆成功返回登陆对象
        UserBean user = userMapper.selectOne(qw);
        if (user == null) { //登陆失败返回null
            return null;
        }
        //同时封装用户的所有权限
        user.setRoleSet(roleMapper.findByUser(user.getId()));
        user.setPowerSet(powerMapper.findByUser(user.getId()));
        //并且记录登陆日志
        journalMapper.add(new JournalBean("用户"+user.getRealName()+"登陆系统"));
        return user;
    }

    @Override
    public IPage<UserBean> findByItem(Integer pageNO, String userName) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        if (userName!=null&&userName.length()!=0){
            qw.like("u_userName",userName);
        }
        return userMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public boolean add(UserBean user, Integer[] roleIdArray) {
        QueryWrapper qw=new QueryWrapper();
        qw.eq("u_userName",user.getUserName());
        UserBean userBean = userMapper.selectOne(qw);
        if (userBean==null){

            userMapper.insert(user);
            userMapper.addRole(user.getId(),roleIdArray);
            return true;
        }

        return false;
    }

    @Override
    public boolean del(Integer id, UserBean loginUser) {

        if (loginUser.getId()!=id){
            userMapper.delRole(id);
            userMapper.deleteById(id);
            return true;
        }
        return false;
    }

    @Override
    public UserBean findById(Integer id) {
        UserBean user = userMapper.selectById(id);
        user.setRoleSet(roleMapper.findByUser(id));
        user.setPowerSet(powerMapper.findByUser(id));

        return user;
    }

    @Override
    public void updateRoles(Integer id, Integer[] roleId) {
        userMapper.delRole(id);
        userMapper.addRole(id, roleId);
    }

    @Override
    public void updatePwd(Integer id, String pwd) {
        UserBean userBean = new UserBean();
        userBean.setId(id);
        userBean.setPwd(pwd);

        userMapper.updateById(userBean);
    }
}
